export const RefreshStatus = {
  Idle: 0,
  CanRefresh: 1,
  Refreshing: 2,
  Completed: 3,
  Failed: 4,
  CanTwoLevel: 5,
  TwoLevelOpening: 6,
  TwoLeveling: 7,
  TwoLevelClosing: 8,
}
Page({
  /**
   * 页面的初始数据
   */
  data: {
    schema: [
      {
        label:'动画时长',
        prop:'duration',
        type:'number',
        value: 300
      },
      {
        label:'动画曲线',
        prop:'easingFunction',
        type:'picker',
        labelOptions:['ease', 'linear', 'ease-in', 'ease-out', 'ease-in-out', 'step-start', 'step-end'], //选项
        valueOptions:['ease', 'linear', 'ease-in', 'ease-out', 'ease-in-out', 'step-start', 'step-end'], //选项对应的值
        value: 'ease'
      },
    ],
    formData: {
      duration: 300,
      easingFunction: 'ease'
    },
    title: '',
    result: '',
    remark: '',
    webUrl: '',
    refreshStatus: '下拉刷新',
  },
  onLoad(options) {
    console.log('options', options)
    wx.setNavigationBarTitle({
      title: options.title,
    })

    this.setData({
      title: options.title,
      zh:options.zh,
      remark: options.remark,
      webUrl: options.webUrl
    })
  },
  getFormData(e) {
    const formData = {}
    this.data.schema.forEach(item => {
      if(item.type === 'number') {
        if(!isNaN(+e.detail.formData[item.prop])) {
          formData[item.prop] = +e.detail.formData[item.prop]
        }
      } else {
        formData[item.prop] = e.detail.formData[item.prop]
      }
    })
    this.setData({
      formData: formData
    })
    console.log('formData', formData);
  },
  buildText(status) {
    switch (status) {
      case RefreshStatus.Idle:
        return '下拉刷新'
      case RefreshStatus.CanRefresh:
        return '松手刷新，下拉进入二楼'
      case RefreshStatus.Refreshing:
        return '正在刷新'
      case RefreshStatus.Completed:
        return '刷新成功'
      case RefreshStatus.Failed:
        return '刷新失败'
      case RefreshStatus.CanTwoLevel:
        return '松手进入二楼'
      default:
        return '进入二楼'
    }
  },
  choose1() {
    wx.createSelectorQuery().select('#scroll-id').node().exec((res) => {
      this.setData({
        result: res
      })
      this.scrollViewContent = res[0].node
    })
  },
  choose2() {
    if(this.scrollViewContent) {
      this.scrollViewContent.triggerRefresh(this.data.formData)
    } else {
      wx.showModal({
        title: '请先创建一个scrollViewContext',
        showCancel: false
      })
    }
  },
  choose3() {
    if(this.scrollViewContent) {
      this.scrollViewContent.closeRefresh()
    } else {
      wx.showModal({
        title: '请先创建一个scrollViewContext',
        showCancel: false
      })
    }
  },
  choose4() {
    if(this.scrollViewContent) {
      this.scrollViewContent.triggerTwoLevel(this.data.formData)
    } else {
      wx.showModal({
        title: '请先创建一个scrollViewContext',
        showCancel: false
      })
    }
  },
  choose5() {
    if(this.scrollViewContent) {
      this.scrollViewContent.closeTwoLevel(this.data.formData)
    } else {
      wx.showModal({
        title: '请先创建一个scrollViewContext',
        showCancel: false
      })
    }
  },
  refresherstatuschange(e) {
    const status = e.detail.status
    console.log('下拉回调');
    this.setData({
      refreshStatus: this.buildText(status)
    })
  },
  bindrefresherrefresh( ) {
    console.log('自定义下拉刷新被触发');
  },
  bindrefresherpulling() {
    console.log('自定义下拉刷新控件被下拉');
  }
})